﻿#region copyright info
//------------------------------------------------------------------------------
// <copyright company="ChaosStudio">
//     Copyright (c) 2002-2010 巧思工作室.  All rights reserved.
//     Contact:		MSN:zhouyu@cszi.com , QQ:478779122
//		Link:				http://www.69sea.com http://www.cszi.com
// </copyright>
//------------------------------------------------------------------------------
#endregion

using System.Web.Caching;
using System.Xml;
using CS.Caching;
using CS.Util.Environment;

namespace CS.Xml.Maps
{
    /// <summary>
    ///   Xml映射文件处理基类
    /// </summary>
    /// 
    /// <description class = "CS.Xml.Maps.XmlMapsBase">
    ///   
    /// </description>
    /// 
    /// <history>
    ///   2010-9-14 13:05:34 , zhouyu ,  创建	     
    ///  </history>
    public  class XmlMapsBase
    {
        protected static CsCache<string, XmlDocument> XmlMapsCache;    //模板转换器缓存

        static XmlMapsBase()
        {
            XmlMapsCache = new CsCache<string, XmlDocument>(600, 100, ExpireModeType.Passive);
        }

        /// <summary>
        /// 根据文件路径载入配置文件信息
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        protected static XmlDocument LoadConfig(string filePath)
        {
            var file = filePath.IndexOf(':') > -1 ? filePath : string.Format("{0}\\{1}",Host.AppBaseDirectory , filePath);
            var key = file.ToLower();

            var xml = XmlMapsCache.Get(key);
            if(xml != null)
                return xml;

            xml = new XmlDataDocument();
            xml.Load(file);
            XmlMapsCache.Add(key, xml, new CacheDependency(file));

            return xml;
        }

        /// <summary>
        /// 获取节点集中相关属性的值
        /// </summary>
        /// <param name="nodes">节点集合</param>
        /// <param name="attName">属性名称</param>
        /// <param name="attNameValue">属性值</param>
        /// <returns></returns>
        protected static XmlNode NodeFind(XmlNodeList nodes, string attName, string attNameValue)
        {
            if (nodes == null) return null;
            for (int i = 0; i < nodes.Count; i++)
            {
                if (nodes[i].Attributes[attName].Value == attNameValue)
                {
                    return nodes[i];
                }
            }
            return null;
        }

    }
}
